const express = require("express")
const router = express.Router()
const shareDB = require("../../../db/share")
const journalDB = require("../../../db/journal.js")
const { sensitiveVer } = require('../../../utils/sensitiveVerification')

// 获取分享文章
router.get("/getAll", async (req, res) => {
  try {
    let { skip, limit } = req.query
    let doc = await shareDB.find({}, {}, {
      skip: skip * 1,
      limit: limit * 1,
      sort: {
        date: -1
      }
    })
    // 没有数据时
    if (!doc.length) {
      return res.send({
        code: 20,
        mes: "没有更多文章啦"
      })
    }
    res.send({
      code: 0,
      mes: "分享文章列表",
      data: doc
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

// 获取文章详情
router.get("/getArticle", async (req, res) => {
  try {
    let { _id, type } = req.query
    // 查询是否有此文章
    let doc = null
    let prev = null
    let next = null
    if (type === "share") {
      doc = await shareDB.findById(_id).populate("author", {
        pass: 0,
        admin: 0,
        allowDelete: 0,
        allowPublish: 0,
        allowUpdate: 0,
        Supertube: 0
      })
      if (!doc) {
        return res.send({
          code: 17,
          mes: "您要查找的文章不存在或已被删除"
        })
      }
      // 添加文章浏览量
      await shareDB.findByIdAndUpdate(_id, {
        $inc: {
          vistor: 1
        }
      })
      // 查询上一篇文章
      prev = await shareDB.findOne({
        date: { $gt: doc.date }
      }, {}, {
        sort: { date: -1 }
      })
      next = await shareDB.findOne({
        date: { $lt: doc.date }
      }, {}, {
        sort: { date: -1 }
      })
    } else if (type === "journal") {
      doc = await journalDB.findById(_id).populate("author", {
        pass: 0,
        admin: 0,
        allowDelete: 0,
        allowPublish: 0,
        allowUpdate: 0,
        Supertube: 0
      })
      if (!doc) {
        return res.send({
          code: 17,
          mes: "您要查找的文章不存在或已被删除"
        })
      }
      // 添加文章浏览量
      await journalDB.findByIdAndUpdate(_id, {
        $inc: {
          vistor: 1
        }
      })
      // 查询上一篇文章
      prev = await journalDB.findOne({
        date: { $gt: doc.date }
      }, {}, {
        sort: { date: -1 }
      })
      next = await journalDB.findOne({
        date: { $lt: doc.date }
      }, {}, {
        sort: { date: -1 }
      })
    }

    if (!prev) {
      return res.send({
        code: 0,
        mes: "文章详情",
        data: doc,
        next: {
          title: next?.title,
          _id: next?._id
        }
      })
    }
    if (!next) {
      return res.send({
        code: 0,
        mes: "文章详情",
        data: doc,
        prev: {
          title: prev.title,
          _id: prev._id
        }
      })
    }
    res.send({
      code: 0,
      mes: "文章详情",
      data: doc,
      prev: {
        title: prev.title,
        _id: prev._id
      },
      next: {
        title: next.title,
        _id: next._id
      }
    })
  } catch (error) {
    console.log(error)
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

// 文章评论
router.post("/commont", async (req, res) => {
  try {
    if (!req.session.userInfo) {
      return res.send({
        code: 5,
        mes: "请先登录"
      })
    }
    let name = req.session.userInfo.name
    let { text, _id, type } = req.body
    if (type === "share") {
      // 判断文章是否存在
      let doc = await shareDB.findById(_id)
      // 判断
      if (!text) {
        return res.send({
          code: 22,
          mes: "评论内容格式有误"
        })
      }
      if (!doc) {
        return res.send({
          code: 17,
          mes: "该文章已被删除，请刷新后重试"
        })
      }

      await sensitiveVer(text)
      await shareDB.findByIdAndUpdate(_id, {
        $push: {
          comment: {
            name,
            text
          }
        }
      })
    } else if (type === "journal") {
      // 判断文章是否存在
      let doc = await journalDB.findById(_id)
      // 判断
      if (!name) {
        return res.send({
          code: 21,
          mes: "用户名格式有误"
        })
      }
      if (!text) {
        return res.send({
          code: 22,
          mes: "评论内容格式有误"
        })
      }
      if (!doc) {
        return res.send({
          code: 17,
          mes: "该文章已被删除，请刷新后重试"
        })
      }
      await sensitiveVer(text)
      await journalDB.findByIdAndUpdate(_id, {
        $push: {
          comment: {
            name,
            text
          }
        }
      })
    }
    res.send({
      code: 0,
      mes: "评论发表成功"
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: error || "服务器异常，请稍后再试"
    })
  }
})

// 获取分享（前三篇 浏览量）
router.get("/getvistor", async (req, res) => {
  try {
    let doc = await shareDB.find({}, {}, {
      sort: {
        vistor: -1
      },
      limit: 3
    })
    res.send({
      code: 0,
      mes: "浏览量前三的浏览量",
      data: doc
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

module.exports = router